home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12735 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  62 lines

  1. Newsgroups: comp.lang.c++
  2. Path: artemis.sto.fdata.se!news
  3. From: Mikael Andersson <prmiand@stog.wmdata.se>
  4. Subject: Re: file processing...
  5. Sender: news@artemis.sto.fdata.se (UseNet NetNews)
  6. Message-ID: <31514F49.3DDF@stog.wmdata.se>
  7. Date: Thu, 21 Mar 1996 12:44:57 GMT
  8. Content-Transfer-Encoding: 7bit
  9. Content-Type: text/plain; charset=us-ascii
  10. References: <4iqr98$i3p@netnews.upenn.edu> <31514C27.58F3@stog.wmdata.se>
  11. Mime-Version: 1.0
  12. X-Mailer: Mozilla 2.0GoldB2 (Win95; I)
  13. Organization: WM Data Infrateknik
  14.  
  15. Mikael Andersson wrote:
  16. > Sonny wrote:
  17. > >
  18. > > I want to read the 1st line of a text file into an array. The only way I know of to
  19. > > detect the end of a line is by checking for "\n", but the code I wrote does not seem
  20. > > be detecting it. Here is the code...
  21. > >
  22. > > #include<fstream.h>
  23. > > #include<iostream.h>
  24. > > #include<String.h>
  25. > >
  26. > > int readf(char array[],char *);
  27. > > int readf (char array[], char *filename)
  28. > > {
  29. > >   int i=0;
  30. > >   char c;
  31. > >
  32. > >   ifstream inFile(filename, ios::in);
  33. > >   while (inFile >> c)
  34. > >     if (c!="\n")      /*This line is giving me problem. It doesn't even compile */
  35. > >       array[i++]=c;
  36. > >     else
  37. > >       break;
  38. > >   return i;
  39. > >
  40. > > }
  41. > >
  42. > > void main(int argc,char *argv[])
  43. > > {
  44. > >   int i;
  45. > >   char array[50];
  46. > >   i=readf(array,"gro");
  47. > >
  48. > >   for(int c=0;c<i;c++)
  49. > >     cout<<array[c];
  50. > >   cout<<"\n";
  51. > > }
  52. > >
  53. > > --Either you can use inFile.getline(buffer_pointer,max_length,'\n') (maybe
  54. > readline I don't remember), or you can change to !='\n' or !=0x0A. I've
  55. > never used the >> so I can be wrong but there's a inFile.read(...,1)
  56. > which would read one character at a time so you could use that instead.
  57. > Hope it helps, Micke.
  58.  
  59. Sorry, that's if you use ifstream, which maybe you should? Micke.
  60.